package com.slipper.system.codeless.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.core.cache.CacheKey;
import com.slipper.core.cache.CacheUtil;
import com.slipper.util.Tool;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.ScriptGroovyDao;
import com.slipper.system.codeless.bo.ScriptGroovyBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class ScriptGroovyService {
    @Resource
    private ScriptGroovyDao scriptGroovyDao;
    @Resource
    private ScriptGroovyHistoryService scriptGroovyHistoryService;
    
    public ScriptGroovyBO queryBy(String id) {
        ScriptGroovyBO scriptGroovy = CacheUtil.get(CacheKey.ScriptGroovy.KEY + id, ScriptGroovyBO.class);
        if (scriptGroovy == null) {
            scriptGroovy = scriptGroovyDao.queryBy(id);
            if (scriptGroovy == null) return null;
            CacheUtil.set(CacheKey.ScriptGroovy.KEY + id, scriptGroovy, 60 * 60);
        }
        return scriptGroovy;
    }
    
    public List<ScriptGroovyBO> queryList(ScriptGroovyBO bo) {
        return scriptGroovyDao.queryList(bo);
    }

    public List<ScriptGroovyBO> queryList(String[] ids) {
        return scriptGroovyDao.queryListByIds(ids);
    }
    
    public PageInfo<ScriptGroovyBO> queryPage(ScriptGroovyBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(scriptGroovyDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(ScriptGroovyBO bo) {
        int result;
        bo.setUpdateTime(System.currentTimeMillis());
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            result = scriptGroovyDao.update(bo);
            CacheUtil.remove(CacheKey.ScriptGroovy.KEY + bo.getId());
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            result = scriptGroovyDao.insert(bo);
        }
        scriptGroovyHistoryService.save(bo);
        return result;
    }

    @WriteTransactional
    public int saveSimple(ScriptGroovyBO bo) {
        int result;
        bo.setUpdateTime(System.currentTimeMillis());
        if (Tool.isNotBlank(bo.getId())) {
            result = scriptGroovyDao.updateSimple(bo);
            CacheUtil.remove(CacheKey.ScriptGroovy.KEY + bo.getId());
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            result = scriptGroovyDao.insert(bo);
        }
        scriptGroovyHistoryService.save(bo);
        return result;
    }

    @WriteTransactional
    public void saveList(List<ScriptGroovyBO> scriptList) {
        if (scriptList == null) return;

        scriptList.forEach(script -> {
            if (scriptGroovyDao.queryBy(script.getId()) != null) {
                script.setUpdateTime(System.currentTimeMillis());
                scriptGroovyDao.update(script);
                CacheUtil.remove(CacheKey.ScriptGroovy.KEY + script.getId());
            } else {
                script.setUpdateTime(System.currentTimeMillis());
                script.setCreateTime(System.currentTimeMillis());
                scriptGroovyDao.insert(script);
            }
        });
    }

    @WriteTransactional
    public int delete(ScriptGroovyBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            CacheUtil.remove(CacheKey.ScriptGroovy.KEY + bo.getId());
        }
        if (bo.getIds() != null) {
            for (String id : bo.getIds()) {
                CacheUtil.remove(CacheKey.ScriptGroovy.KEY + id);
            }
        }
        return scriptGroovyDao.delete(bo);
    }

    public long queryTotal(ScriptGroovyBO bo) {
        PageHelper.startPage(0, 0);
        return new PageInfo<>(scriptGroovyDao.queryList(bo)).getTotal();
    }
}
